/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmijcoltrane;

/**
 *
 * @author Felipe Gutierrez
 */


import java.util.Stack;

import net.sf.jColtrane.annotations.methods.BeforeElement;
import net.sf.jColtrane.annotations.methods.EndElement;
import net.sf.jColtrane.annotations.methods.StartElement;
import net.sf.jColtrane.handler.ContextVariables;
import net.sf.jColtrane.handler.SAXEvent;


import xmibase.XmiAssociation;
import xmibase.XmiAssociationEnd;
import xmibase.XmiAttribute;
import xmibase.XmiClass;
import xmibase.XmiPackage;
import xmibase.XmiTagDefinition;
import xmibase.XmiTagValue;

public class XmiGetAssociation {

	public XmiPackage rootPackage;
	public Stack elements;
	public boolean debug = false;

	public XmiGetAssociation(XmiPackage packages, Stack elements) {

		this.elements = elements;
		this.rootPackage = packages;

	}

	public void printDebug(String msg) {
		if (debug) {
			System.out.println(msg);
		}
	}

	/**
	 * Start a UML:Association at UML:Package
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:Namespace.ownedElement")
	@StartElement(tag = "UML:Association")
	public void startElementAssociation(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();
		XmiAssociation xmiAssociation = new XmiAssociation();

		printDebug("     Start Association: "
				+ event.getAtributesHolder().getValue("xmi.id"));

		xmiAssociation.setId(event.getAtributesHolder().getValue("xmi.id"));

		this.elements.push(xmiAssociation);

		contextVariables.putInGeneralUseMap(xmiAssociation.getId(),
				xmiAssociation);

	}

	@BeforeElement(elementDeep = 1, tag = "UML:Namespace.ownedElement")
	@EndElement(tag = "UML:Association")
	public void endElementAssociation(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();

		XmiAssociation tempAssociation = (XmiAssociation) this.elements.peek();

		this.elements.pop();

		this.rootPackage.addAssociation(tempAssociation);

		printDebug("     End Association.");

	}

	@BeforeElement(elementDeep = 1, tag = "UML:Association.connection")
	@StartElement(tag = "UML:AssociationEnd")
	public void startElementAssociationEnd(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();

		printDebug("            Start AssociationEnd");

		XmiAssociationEnd xmiAssociationEnd = new XmiAssociationEnd();
		{
			xmiAssociationEnd.setId(event.getAtributesHolder().getValue(
					"xmi.id"));
			xmiAssociationEnd.setNavegable(Boolean.parseBoolean(event
					.getAtributesHolder().getValue("isNavigable")));
			xmiAssociationEnd.setSpecification(Boolean.parseBoolean(event
					.getAtributesHolder().getValue("isSpecification")));
			xmiAssociationEnd.setVisibility(event.getAtributesHolder()
					.getValue("visibility"));

			xmiAssociationEnd.setName(event.getAtributesHolder().getValue(
					"name"));
			xmiAssociationEnd.setAggregation(event.getAtributesHolder()
					.getValue("aggregation"));
			xmiAssociationEnd.setTargetScope(event.getAtributesHolder()
					.getValue("targetScope"));
			xmiAssociationEnd.setChangeability(event.getAtributesHolder()
					.getValue("changeability"));

		}
		this.elements.push(xmiAssociationEnd);
		contextVariables.getGeneralUseMap().put(xmiAssociationEnd.getId(),
				xmiAssociationEnd);

	}

	/**
	 * UML:AssociationEnd at UML:Assciation
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:Association.connection")
	@EndElement(tag = "UML:AssociationEnd")
	public void endElementAssociationEnd(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();

		XmiAssociationEnd xmiAssociationEnd = (XmiAssociationEnd) this.elements
				.peek();

		this.elements.pop();
		XmiAssociation tempAssociation = (XmiAssociation) this.elements.peek();

		tempAssociation.addAssociationEnd(xmiAssociationEnd);

		printDebug("            End AssociationEnd");
	}

	// Elements of Class
	/**
	 * UML:MultiplicityRange at UML:AssociationEnd
	 */
	@BeforeElement(elementDeep = 3, tag = "UML:AssociationEnd.multiplicity")
	@StartElement(tag = "UML:MultiplicityRange")
	public void startElementMultiplicityRange(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();

		XmiAssociationEnd xmiAssociationEnd = (XmiAssociationEnd) this.elements
				.peek();

		{
			xmiAssociationEnd.setLowerMultiplicity(Integer.parseInt(event
					.getAtributesHolder().getValue("lower")));
			xmiAssociationEnd.setUpperMultiplicity(Integer.parseInt((event
					.getAtributesHolder().getValue("upper"))));
		}

	}

	/**
	 * UML:Class at UML:AssociationEnd
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:AssociationEnd.participant")
	@StartElement(tag = "UML:Class")
	public void startElementClass(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();
		XmiAssociationEnd xmiAssociationEnd = (XmiAssociationEnd) this.elements
				.peek();

		XmiClass xmiClass = new XmiClass();

		{
			xmiClass.setId(event.getAtributesHolder().getValue("xmi.idref"));
			xmiAssociationEnd.setXmiClass(xmiClass);
		}

	}


}
